package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.Department;
import com.ktgj.customer.domain.vo.DepartmentsVo;
import com.ktgj.customer.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 部门Controller
 *
 * @author ruoyi
 * @date 2021-07-27
 */
@ControllerAdvice
@RestController
@RequestMapping("/department")
public class DepartmentController extends BaseController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询部门列表
     */
    //@PreAuthorize(hasPermi = "system:department:list")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Department department) {
        startPage();
        List<Department> list = departmentService.selectDepartmentList(department);
        return getDataTable(list);
    }
    @PostMapping("/getDeptName")
    public TableDataInfo getDeptName(@RequestBody Department department) {
        List<Department> list = departmentService.selectDepartmentList(department);
        return getDataTable(list);
    }

    /**
     * 查询部门列表
     */
    @PostMapping("/departmentlist")
    public AjaxResult departmentlist(@RequestBody Department department) {
        List<Department> list = departmentService.selectDepartmentList(department);
        return AjaxResult.success(list);
    }

    /**
     * 导出部门列表
     */
    //@PreAuthorize(hasPermi = "system:department:export")
    @Log(title = "部门", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Department department) throws IOException {
        List<Department> list = departmentService.selectDepartmentList(department);
        ExcelUtil<Department> util = new ExcelUtil<Department>(Department.class);
        util.exportExcel(response, list, "部门数据");
    }

    /**
     * 获取部门详细信息
     */
    //@PreAuthorize(hasPermi = "system:department:query")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getDepartmentInfo(@PathVariable("deptId") Long deptId) {
        return AjaxResult.success(departmentService.selectDepartmentById(deptId));
    }

    /**
     * 获取父类部门名
     * @param map
     * @return
     */
    @PostMapping("/getParentDepartMentName")
    public AjaxResult getParentDepartMentName(@RequestBody Map<String,Object> map) {
        Long deptId=new Long (map.get("parentDeptId").toString());

        return AjaxResult.success(departmentService.selectDepartmentById(deptId));
    }

    /**
     * 新增部门
     */
    //@PreAuthorize(hasPermi = "system:department:add")
    @Log(title = "部门", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Department department) {
        //非空验证
        if (department.getDeptName().equals("") || department.getDeptName() == null ||
                department.getDeptDesc().equals("") || department.getDeptDesc() == null ||
                department.getParentDeptId().equals("0l") || department.getParentDeptId() == null) {
            return AjaxResult.error("参数均不能为空");
        }
        return toAjax(departmentService.insertDepartment(department));
    }

    /**
     * 修改部门
     */
    // @PreAuthorize(hasPermi = "system:department:edit")
    @Log(title = "部门", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Department department) {
        if (department.getParentDeptId().equals(0))
            return AjaxResult.error("根节点不能编辑");
        return toAjax(departmentService.updateDepartment(department));
    }

    /**
     * 删除部门
     */
    // @PreAuthorize(hasPermi = "system:department:remove")
    @Log(title = "部门", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptIds}")
    public AjaxResult remove(@PathVariable Long[] deptIds) {
        return toAjax(departmentService.deleteDepartmentByIds(deptIds));
    }

    /**
     * 查询部门树状图
     */
    // @PreAuthorize(hasPermi = "system:department:remove")
    @PostMapping("/getDepartmentList")
    public AjaxResult getDepartmentList(@RequestBody Department department) {
        return AjaxResult.success(departmentService.departmentList(department));
    }

    /**
     * 停用部门
     */
    @PostMapping("/deactivate")
    public AjaxResult deactivate(@RequestBody Map<String, Object> map) {
        Long deptId = new Long(map.get("deptId").toString());
        Integer i = departmentService.deactivate(deptId);
        Integer count = departmentService.queryEmployeeCountByDeptId(deptId);
        if (i > 0 && count > 0) {
            return AjaxResult.success("操作成功,该部门下存在员工编制,请即使处理");
        } else if (i > 0 && count == 0) {
            return AjaxResult.success("操作成功");
        }
        return AjaxResult.error("操作失败");

    }

    /**
     * 启用部门
     */
    @PostMapping("/enable")
    public AjaxResult enable(@RequestBody Map<String, Object> map) {
        Long deptId = new Long(map.get("deptId").toString());
        return toAjax(departmentService.enable(deptId));
    }

    /**
     * 部门树
     */
    @PostMapping("getAll")
    public AjaxResult getAll() {
        return AjaxResult.success(departmentService.getAll());
    }

    /**
     * 查询员工所在部门（远程）
     * */
    @PostMapping("/department")
    public Department department(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        Department department = departmentService.department(id);
        return department;
    }

    /**
     * id获取部门名称（远程）
     * */
    @PostMapping("/deptName")
    public Department deptName(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        Department department = departmentService.deptName(id);
        return department;
    }

    /**
     * 部门列表
     * */
    @PostMapping("/departments")
    public AjaxResult departments(){
        List<Department> departmentList = departmentService.departments();
        return AjaxResult.success(departmentList);
    }
}
